package com.egg.auth.utils;


import cn.hutool.core.util.ObjectUtil;
import com.egg.auth.service.role.IRoleService;
import com.egg.auth.service.user.IUserService;
import com.egg.common.core.exception.CustomException;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

/**
 * Optional 工具类
 *
 * @author east
 */
public class PasswordUtil {


    private static BCryptPasswordEncoder bCryptPasswordEncoder;


    public void setBCryptPasswordEncoder(BCryptPasswordEncoder bCryptPasswordEncoder) {
        this.bCryptPasswordEncoder = bCryptPasswordEncoder;
    }

    /**
     * 密码加密
     *
     * @param originalPassword 原始密码
     * @return
     */
    public static String encryptionPassword(String originalPassword) {
        if (ObjectUtil.isEmpty(originalPassword)) {
            throw new CustomException("加密密码不允许为空");
        }
        return bCryptPasswordEncoder.encode(originalPassword);
    }

    /**
     * 匹配密码
     *
     * @param originalPassword 原始密码
     * @param encodedPassword  被比较密码
     * @return
     */
    public static boolean equalsPassword(String originalPassword, String encodedPassword) {
        return bCryptPasswordEncoder.matches(originalPassword, encodedPassword);
    }


}